package com.homework.HomeAcount.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.homework.HomeAcount.entity.Acount;
import com.homework.HomeAcount.entity.CurrentUser;
import com.homework.HomeAcount.service.AcountService;
import com.homework.HomeAcount.util.ShiroUtil;

@Controller
public class AcountController {
	
	@Autowired
	AcountService acountService;
	
	@RequestMapping("/acount")
	@RequiresPermissions("account:*")
	public String loadAcount(Model model) {
		List<Map<String, Object>> list = acountService.getAcountForwhoList();
		model.addAttribute("forwho", list);
		return "acount/acount";
	}
	
	@RequestMapping("/acount/loadTable")
	@ResponseBody
	public Map<String, Object> loadTable(String limit,String offset,HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, Object> map = new HashMap<String, Object>();
		String name = StringUtils.trimToNull(request.getParameter("name"));
		String type = StringUtils.trimToNull(request.getParameter("type"));
		String forwho = StringUtils.trimToNull(request.getParameter("forwho"));
		String balance = StringUtils.trimToNull(request.getParameter("balance"));
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if(null != name) {
			//中文乱码解决
			name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
		}
		paramMap.put("name", name);
		paramMap.put("type", type);
		paramMap.put("forwho", forwho);
		paramMap.put("balance", balance);
		int pageSize = Integer.valueOf(limit);
		int pageNumber = Integer.valueOf(offset);
		int total = acountService.getListSize(paramMap);
		List<Map<String, Object>> list = acountService.loadAcountList(pageSize,pageNumber,paramMap);
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	@RequestMapping("/acount/loadAcountType")
	@ResponseBody
	public List<Map<String, Object>> loadAcountType(String balance) {
		return acountService.getAcountTypeList(balance);
	}
	
	@RequestMapping("/acount/loadAcountById")
	@ResponseBody
	public Map<String, Object> loadAcountById(String id) {
		return acountService.loadAcountById(id);
	}
	
	@RequestMapping("/acount/addAcount")
	@ResponseBody
	public Map<String, Object> addAcount(Acount acount) {
		Map<String, Object> map = new HashMap<String, Object>();
		CurrentUser currentUser = ShiroUtil.getCurrentUser();
		acount.setCreator(currentUser.getId());
		try {
			acountService.addAcount(acount);
			map.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", true);
		}
		return map;
	}
	
	@RequestMapping("/acount/updateAcount")
	@ResponseBody
	public Map<String, Object> updateAcountById(Acount acount) {
		Map<String, Object> map = new HashMap<String, Object>();
		CurrentUser currentUser = ShiroUtil.getCurrentUser();
		acount.setModifier(currentUser.getId());
		try {
			acountService.updateAcountById(acount);
			map.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", false);
		}
		return map;
	}
	
	@RequestMapping("/acount/deleteAcount")
	@ResponseBody
	public Map<String, Object> deleteAcount(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			acountService.deleteAcountById(id);
			map.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", false);
		}
		return map;
	}
}
